package cool.taomu.toolkit.jsch.service

import com.jcraft.jsch.JSch
import cool.taomu.toolkit.jsch.entity.JschConfigEntity.Proxy
import java.util.Hashtable
import java.util.List
import org.eclipse.xtend.lib.annotations.Accessors
import org.slf4j.LoggerFactory
import com.jcraft.jsch.Session

interface IJschConnection{
	def Session resource();
}

@Accessors
class JschConnection implements IJschConnection{
	val static LOG = LoggerFactory.getLogger(JschConnection);
	String host;
	String username;
	String password;
	int port;
	int timeout = 60000000;
	Hashtable<String, String> sshconfig = new Hashtable();
	List<Proxy> proxy

	new() {
	}

	new(String host, String uname, String passwd, int port) {
		this.host = host;
		this.username = uname;
		this.password = passwd;
		this.port = port;
	}

	override resource() {
		try {
			var jsch = new JSch();
			var session = jsch.getSession(this.username, this.host, this.port);
			session.password = this.password;
			if (!sshconfig.containsKey("StrictHostKeyChecking")) {
				sshconfig.put("StrictHostKeyChecking", "no");
			}
			session.setConfig(sshconfig);
			// id_rsa jsch.addIdentity("","");
			session.timeout = this.timeout;
			session.connect
			if (session.isConnected) {
				LOG.info("链接成功")
			} else {
				LOG.info("链接失败")
			}
			return session;
		} catch (Exception ex) {
			LOG.info("失败", ex)
		}
		return null;
	}
}
